package com.yswee.mallnavi.model;

import android.text.TextUtils;
import android.widget.Toast;

import com.yswee.framework.Application;
import com.yswee.framework.data.BaseArrayData;
import com.yswee.framework.model.PageType;
import com.yswee.framework.net.ITaskContext;
import com.yswee.framework.net.http.Callback;
import com.yswee.framework.net.http.Entity;
import com.yswee.framework.net.http.HttpType;
import com.yswee.framework.utils.CacheUtils;
import com.yswee.framework.utils.FileUtils;
import com.yswee.mallnavi.Preference;
import com.yswee.mallnavi.R;
import com.yswee.mallnavi.context.ContextConstant;
import com.yswee.mallnavi.entity.common.CompanyEntity;

import java.io.File;
import java.net.HttpURLConnection;

public abstract class BaseListModel<T> extends com.yswee.framework.model.BaseListModel<T> {

	public static final int PAGESIZE = 15;
	
	@Override
	protected int getFirstPageIndex() {
		return 0;
	}
	
	@Override
	protected HttpType getHttpType() {
		return HttpType.Get;
	}
	
	protected String getCacheFileName() {
		return null;
	}
	
	protected String getCacheFilePath() {
		if(TextUtils.isEmpty(getCacheFileName())) {
			return null;
		}
		return CacheUtils.FOLDER_JSON + File.separator + getCacheFileName();
	}
	
	protected String getDebugJson() {
		return null;
	}
	
	@Override
	public boolean getCacheData(Callback<BaseArrayData<T>> callback) {
		super.getCacheData(callback);
		if(!TextUtils.isEmpty(getCacheFileName())) {
			String json = FileUtils.getStringFromCachePath(getCacheFilePath());
			if(!TextUtils.isEmpty(json)) {
				Entity<BaseArrayData<T>> entity = new Entity<BaseArrayData<T>>();
				entity.setParsedData(getParser().parseData(json));
				callback.onFinish(entity);
			}
		}
		return true;
	}
	
	public BaseListModel(ITaskContext context) {
		super(context);
	}

	@Override
	protected CallbackProxy<T> getCallBackProxy(Callback<BaseArrayData<T>> c) {
		return new CallbackProxy<T>(BaseListModel.this, c);
	}
	
	public static class CallbackProxy<T> extends com.yswee.framework.model.BaseListModel.CallbackProxy<T> {
		
		public CallbackProxy(BaseListModel<T> model, Callback<BaseArrayData<T>> callback) {
			super(model, callback);
		}
		
		@Override
		public void onError(Entity<BaseArrayData<T>> entity) {
			if(entity.getEntityStatus() == Entity.NETINVALID) {
				Toast.makeText(Application.get(), R.string.network_invalid, Toast.LENGTH_SHORT).show();
			}
			super.onError(entity);
		}
		
		@Override
		public void onBeginConnection(HttpURLConnection connection) {
			super.onBeginConnection(connection);
			CompanyEntity company = Preference.getCompany();
			if(company != null) {
				connection.addRequestProperty("__tenant", company.id);
				if (company.sites != null && company.sites.size() > 0) {
					connection.addRequestProperty("SiteId", company.sites.get(0).id);
				}
			}
			connection.addRequestProperty("ActionToken", ContextConstant.ACTIONTOKEN);
			connection.addRequestProperty("device", ContextConstant.DEVICETYPE);
			connection.addRequestProperty("v", ContextConstant.VERSION);
		}
		
		@Override
		public BaseArrayData<T> onProcessData(Entity<BaseArrayData<T>> entity, String result) {
			if(getPageType() == PageType.FirstPage) {
				String cacheFile = ((BaseListModel<T>) mModel).getCacheFilePath();
				if(!TextUtils.isEmpty(cacheFile)) {
					FileUtils.saveString2CachePath(result, cacheFile);
				}
			}
			return super.onProcessData(entity, result);
		}
		
		@Override
		protected String getDebugJson() {
			return ((BaseListModel<T>)mModel).getDebugJson();
		}
	}
	
}
